package com.example.growthmuseum.controllers;

import com.example.growthmuseum.daos.IGoodsDao;
import com.example.growthmuseum.daos.IUserDao;
import com.example.growthmuseum.models.Goods;
import com.example.growthmuseum.models.Result;
import com.example.growthmuseum.models.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Roman on 2016/10/30.
 */
@RestController
@RequestMapping("/growth-museum/user")
public class UserController {

    private static final Logger LOGGER =
            LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    private IUserDao userDao;

    /**
     * 添加用户.
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result<Integer>>
    addUser(@RequestBody User user) {
        LOGGER.info("receive request>>addUser:[{}] ", user.toString());
        Result<Integer> result = new Result<>();
        result.setCode(Result.SUCCESS);
        try {
            result.setBean(userDao.addUser(user));
            result.setMessage("注册用户成功");
        } catch (RuntimeException e) {
            result.setCode(Result.FAILED);
            result.setMessage(e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 修改用户.
     */
    @RequestMapping(value = "/updateUser", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity<Result<Integer>>
    updateUser(@RequestBody User user) {
        LOGGER.info("receive request>>updateUser:[{}] ", user.toString());
        Result<Integer> result = new Result<>();
        result.setCode(Result.SUCCESS);
        try {
            result.setBean(userDao.updateUser(user));
            result.setMessage("修改用户成功");
        } catch (RuntimeException e) {
            result.setCode(Result.FAILED);
            result.setMessage(e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 用户登录.
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result<Integer>>
    login(@RequestBody User user) {
        LOGGER.info("receive request>>login:[{}] ", user.toString());
        Result<Integer> result = new Result<>();
        result.setCode(Result.SUCCESS);
        try {
            User tmp = userDao.findUser(user.getUsername(), user.getPwd());
            if (tmp.getId() == null) {
                result.setCode(Result.SUCCESS);
                result.setMessage("用户名/密码错误");
            } else {
                result.setCode(Result.SUCCESS);
                result.setBean(tmp.getId());
                result.setMessage("登录成功");
            }
        } catch (RuntimeException e) {
            result.setCode(Result.FAILED);
            result.setMessage(e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
}
